
#include "qyLogger.h"
#include "../common/qyCommon.h"
#include "../memsys/qyString.h"
#include "qyLogHttpString.h"

#include <ctime>
using namespace std;

//-----------------------------------------------------------------------
qyLogger::qyLogger()
: m_bSwitchLogTime( true )
, m_bFlagFinishLine( true )
, m_bInited( false )
{

}

//-----------------------------------------------------------------------
qyLogger::qyLogger( bool bSwitchLogTime, bool m_bFlagFinishLine )
: m_bSwitchLogTime( bSwitchLogTime )
, m_bFlagFinishLine( m_bFlagFinishLine )
, m_bInited( false )
{

}

//-----------------------------------------------------------------------
qyLogger::~qyLogger()
{
	m_file.close();
}

//-----------------------------------------------------------------------
bool qyLogger::SetLogFile(const std::string &fileName)
{
	if ( m_bInited )
		return false;

	std::string logfileName = fileName + ".html";
	m_file.open( logfileName.c_str() );
	m_bInited = true;
	return true;
}

//-----------------------------------------------------------------------
void qyLogger::LogHead( const char *style )
{
	if ( style == NULL )
	{
		m_file << LOG_STRING_BLACK_0;
	}
	else
	{
		m_file << style;
	}
}

//-----------------------------------------------------------------------
void qyLogger::LogTime()
{
	time_t	ctTime;
	time( &ctTime );
	tm time;
#ifdef _WIN32
	localtime_s( &time, &ctTime );
#else
    localtime_r( &ctTime, &time );
#endif
	m_file	<< setw(2) << setfill('0') << time.tm_hour << ":" 
		<< setw(2) << setfill('0') << time.tm_min  << ":"
		<< setw(2) << setfill('0') << time.tm_sec  << " ";
}

//-----------------------------------------------------------------------
void qyLogger::LogEnd()
{
	m_file << " " << "</font><br>\n";
	if( m_bFlagFinishLine )
	{
		m_file << endl;
	}
	m_file.flush();
}

//-----------------------------------------------------------------------
void qyLogger::LogMsg(const string &message, bool ifline/* = true*/, const char *style )
{
	LogHead( style );
	if ( m_bSwitchLogTime && m_bFlagFinishLine )
	{
		LogTime();
	}

	m_file << message;
	m_bFlagFinishLine = ifline;
	LogEnd();
}

//-----------------------------------------------------------------------
void qyLogger::LogMsg(const double message, bool ifline /* = true */, const char *style)
{
	LogHead( style );
	if ( m_bSwitchLogTime && m_bFlagFinishLine )
	{
		LogTime();
	}

	m_file << message;
	m_bFlagFinishLine = ifline;
	LogEnd();
}

//-----------------------------------------------------------------------
void qyLogger::LogMsg(const long message, bool ifline /* = true */, const char *style)
{
	LogHead( style );
	if ( m_bSwitchLogTime && m_bFlagFinishLine )
	{
		LogTime();
	}

	m_file << message;
	m_bFlagFinishLine = ifline;
	LogEnd();
}

//-----------------------------------------------------------------------
void qyLogger::LogMsg(const unsigned long message, bool ifline /* = true */, const char *style)
{
	LogHead( style );
	if ( m_bSwitchLogTime && m_bFlagFinishLine )
	{
		LogTime();
	}

	m_file << message;
	m_bFlagFinishLine = ifline;
	LogEnd();
}

